import { entryPoolList, prePoolList, sufPoolList } from "./equipment";
import { getRandomOne, random, keepDecimal, growthCurve } from "@/util/util";
import { propertyAttr } from "../character";
import { v4 as uuidv4 } from "uuid";
import {
  equipGrowth as growth,
  equipAffixFac as affix,
  equipFloatF
} from "./equipConst";
// 图片
import imgCatelog from "@/assets/img/catelog";

// 装备词条/属性
interface EquipEntry {
  name: string; // 属性名称
  type: string; // 属性类型
  isPercent: boolean; // 是否百分比加成
  value: number; // 加成值
}
// 装备前缀 装备后缀
interface EquipAffix {
  affName: string;
  affProps: EquipEntry[];
}
// 装备主体
interface EquipMain {
  name: string;
  describe: string;
  type: string;
  props: EquipEntry[];
  grade: number;
  level: number;
  icon: number;
  price: number;
}

// 计算属性等级和品级加成及浮动
const attrAdditionFloat = (
  value: number,
  type: string,
  level: number,
  grade: number,
  floatF: number
) => {
  if ([propertyAttr.AS, propertyAttr.CRR, propertyAttr.CRD].includes(type)) {
    return (
      value *
      (1 + grade * 0.1) *
      (1 + random(-floatF * 100, floatF * 100) / 100)
    );
  }
  return (
    value *
    growthCurve(level, growth) *
    (1 + grade * 0.1) *
    (1 + random(-floatF * 100, floatF * 100) / 100)
  );
};
const propFloat = (arr: EquipEntry[], floatF: number) => {
  arr.forEach(m => {
    const value = m.value * (1 + random(-floatF * 100, floatF * 100) / 100);
    if (m.isPercent) {
      m.value = keepDecimal(value, 2);
    } else {
      if (
        [propertyAttr.AS, propertyAttr.CRD, propertyAttr.CRR].includes(m.type)
      ) {
        m.value = keepDecimal(value, 2);
      } else {
        m.value = keepDecimal(value, 0);
      }
    }
  });
  return arr;
};
const attrSolveFn = (arr: EquipEntry[], options: any) => {
  const { level, grade, floatF } = options;
  arr.forEach(m => {
    const value = attrAdditionFloat(m.value, m.type, level, grade, floatF);
    if (m.isPercent) {
      m.value = keepDecimal(value, 2);
    } else {
      if (
        [propertyAttr.AS, propertyAttr.CRD, propertyAttr.CRR].includes(m.type)
      ) {
        m.value = keepDecimal(value, 2);
      } else {
        m.value = keepDecimal(value, 0);
      }
    }
  });
};
// 装备实体类
class Equipment {
  constructor(equipMain: EquipMain, floatF = equipFloatF) {
    const {
      grade,
      level,
      name,
      props,
      type,
      describe,
      icon,
      price
    } = equipMain;
    this.grade = grade;
    this.type = type;
    this.level = level;
    this.props = propFloat(props, floatF);
    this.icon = icon;
    this.price = price;
    this.describe = describe;
    const options = { level, grade, floatF };
    if (Math.random() <= affix) {
      this.pre = getRandomOne(prePoolList);
    }
    if (Math.random() <= affix) {
      this.suf = getRandomOne(sufPoolList);
    }
    let times = grade;
    while (times) {
      this.entry.push(getRandomOne(entryPoolList));
      times--;
    }
    this.pre && attrSolveFn(this.pre.affProps, options);
    this.suf && attrSolveFn(this.suf.affProps, options);
    attrSolveFn(this.entry, options);
    this.name = `${this.pre ? this.pre.affName + "之" : ""}${name}${
      this.suf ? "·" + this.suf.affName : ""
    }`;
    this.uuid = uuidv4();
  }
  id?: number;
  uuid = "";
  name = "";
  describe = "";
  grade = 0;
  level = 0;
  icon = 0;
  type = "";
  price = 0;
  props: EquipEntry[] = [];
  pre?: EquipAffix = undefined;
  suf?: EquipAffix = undefined;
  entry: EquipEntry[] = [];
}
const equiptoPackItem = (equipment: Equipment): PackItem => {
  const { id, name, icon, price, grade } = equipment;
  return {
    id: id || 0,
    name: name,
    img: imgCatelog.equip[icon],
    count: 1,
    type: "equipment",
    price,
    isLock: false,
    grade
  };
};

/**
 * 装备tooltip传入数据替代类型
 * value @params
 * 只需传入value
 */
class EquipDataType {
  flag = false;
  value?: Equipment;
}
export {
  EquipEntry,
  EquipAffix,
  EquipMain,
  Equipment,
  equiptoPackItem,
  EquipDataType
};
